/*
 * ============================================================================
 * = COPYRIGHT
 *      PAX Computer Technology(Shenzhen) CO., LTD PROPRIETARY INFORMATION
 *   This software is supplied under the terms of a license agreement or
 *   nondisclosure agreement with PAX  Technology, Inc. and may not be copied
 *   or disclosed except in accordance with the terms in that agreement.
 *      Copyright (C) 2021-? PAX Technology, Inc. All rights reserved.
 * Description: // Detail description about the function of this module,
 *             // interfaces with the other modules, and dependencies.
 * Revision History:
 * Date	                 Author	                Action
 * 20210331  	          wujs           	    Create
 * ============================================================================
 */
package com.example.glidex;

import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.ModelLoaderFactory;
import com.bumptech.glide.load.model.MultiModelLoaderFactory;
import com.bumptech.glide.signature.ObjectKey;

import java.io.InputStream;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class UrlsLoader implements ModelLoader<ImgUrlWrapper, InputStream> {
    @Nullable
    @Override
    public LoadData<InputStream> buildLoadData(@NonNull ImgUrlWrapper wrapper, int width, int height,
            @NonNull Options options) {
        return new LoadData<>(new ObjectKey(wrapper), new UrlsFetcher(wrapper.getUrls()));
    }

    @Override
    public boolean handles(@NonNull ImgUrlWrapper wrapper) {
        // 只允许带有http和https的链接地址
        for (String url : wrapper.getUrls()) {
            if (!url.startsWith("http") || !url.startsWith("https")) {
                return false;
            }
        }
        return true;
    }

    public static class Factory implements ModelLoaderFactory<ImgUrlWrapper, InputStream> {
        @NonNull
        @Override
        public ModelLoader<ImgUrlWrapper, InputStream> build(
                @NonNull MultiModelLoaderFactory multiFactory) {
            return new UrlsLoader();
        }
        @Override
        public void teardown() {

        }
    }
}
